<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// HTTP
define('HTTP_SERVER', 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/');
define('HTTP_INET', 'http://' . $_SERVER['HTTP_HOST'] . rtrim(rtrim(dirname($_SERVER['SCRIPT_NAME']), 'install'), '/.\\'). '/');

// DIR
define('DIR_APPLICATION', str_replace('\'', '/', realpath(dirname(__FILE__))) . '/');
define('DIR_SYS', str_replace('\'', '/', realpath(dirname(__FILE__) . '/../')) . '/sys/');
define('DIR_LIB', str_replace('\'', '/', realpath(dirname(__FILE__) . '/../')) . '/lib/');
define('DIR_INET', str_replace('\'', '/', realpath(DIR_APPLICATION . '../')) . '/');
define('DIR_LANGUAGE', DIR_APPLICATION . 'language/');
define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
 

// Upgrade
$install = false;

if (filesize('../config.php') > 0) {
	$install = true;	
}



// Startup
// System
require_once(DIR_SYS . 'action.php'); 
require_once(DIR_SYS . 'controller.php');
require_once(DIR_SYS . 'front.php');
require_once(DIR_SYS . 'loader.php'); 
require_once(DIR_SYS . 'model.php');
require_once(DIR_SYS . 'registry.php');



// Lib

require_once(DIR_LIB . 'url.php');
require_once(DIR_LIB . 'config.php');
require_once(DIR_LIB . 'db.php');
require_once(DIR_LIB . 'document.php');
require_once(DIR_LIB . 'image.php');
require_once(DIR_LIB . 'language.php');
require_once(DIR_LIB . 'log.php');
require_once(DIR_LIB . 'mail.php');
require_once(DIR_LIB . 'request.php');
require_once(DIR_LIB . 'response.php');
require_once(DIR_LIB . 'session.php');
require_once(DIR_LIB . 'template.php');
require_once(DIR_LIB . 'pagination.php');
require_once(DIR_LIB . 'utf8.php');

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Request
$request = new Request();
$registry->set('request', $request);

// Response
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=UTF-8');
$registry->set('response', $response);

// Document
$document = new Document();
$registry->set('document', $document);

// Front Controller
$controller = new Front($registry);

// Router
if (isset($request->get['route'])) {
	$action = new Action($request->get['route']);
} elseif ($install) {
	$action = new Action('installed');
} else {
	$action = new Action('step_1');
}

// Dispatch
$controller->dispatch($action, new Action('not_found'));

// Output
$response->output();
?>